import React from "react";
export default class CmtBox extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  render() {
    return (
      <div>
        <label>评论人:</label>
        <br />
        <input type="text" ref="user" />
        <br />
        <label>评论内容:</label>
        <br />
        <textarea name="" id="" cols="30" rows="3" ref="content"></textarea>
        <br />
        <button onClick={this.postComment}>发表</button>
      </div>
    );
  }
  postComment = () => {
    //获取到数据
    console.log("post");
    var cmtIofo = {
      user: this.refs.user.value,
      content: this.refs.content.value,
    };
    //将数据拼接unshift到localstorage
    var list = JSON.parse(localStorage.getItem("cmts") || "[]");
    list.unshift(cmtIofo);
    localStorage.setItem("cmts", JSON.stringify(list));
    this.refs.user.value = this.refs.content.value = "";
    this.props.reload();
  };
}
